package cn.school.studyManage.service.impl;

import cn.school.studyManage.dao.StudentBehaviorCfgClassDao;
import cn.school.studyManage.dao.StudentBehaviorCfgDao;
import cn.school.studyManage.dto.StudentBehaviorCfgClassDTO;
import cn.school.studyManage.entity.StudentBehaviorCfg;
import cn.school.studyManage.entity.StudentBehaviorCfgClass;
import cn.school.studyManage.service.StudentBehaviorCfgClassService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentBehaviorCfgClassServiceImpl extends ServiceImpl<StudentBehaviorCfgClassDao, StudentBehaviorCfgClass> implements StudentBehaviorCfgClassService {

    @Autowired
    StudentBehaviorCfgClassDao cfgClassDao;

    @Autowired
    StudentBehaviorCfgDao cfgDao;

    @Override
    @Transactional
    public Boolean createOrUpdateCfgClassBatch(StudentBehaviorCfgClassDTO dto) {
        Long cfgId = dto.getCfgId();
        List<Long> classIdList = dto.getClassIdList();
        if(cfgId != null){
            //如果提交的数据无classId代表配置将不在绑定班级
            if(classIdList == null || classIdList.size() <= 0){
                QueryWrapper<StudentBehaviorCfgClass> wrapper = new QueryWrapper<>();
                wrapper.eq("cfg_id",cfgId);
                int flag = cfgClassDao.delete(wrapper);
                if(flag > 0){
                    StudentBehaviorCfg studentBehaviorCfg = new StudentBehaviorCfg();
                    studentBehaviorCfg.setId(cfgId);
                    studentBehaviorCfg.setStatus(0);
                    cfgDao.updateById(studentBehaviorCfg);
                }
                return true;
            }else{
                //提交的数据不为空的情况下，先删除数据，然后重新插入
                QueryWrapper<StudentBehaviorCfgClass> wrapper = new QueryWrapper<>();
                wrapper.eq("cfg_id",cfgId);
                int delete = cfgClassDao.delete(wrapper);
                //删除影响行数小于1，当前配置下无班级绑定，此时绑定班级需更新状态
                if(delete < 1) {
                    StudentBehaviorCfg studentBehaviorCfg = new StudentBehaviorCfg();
                    studentBehaviorCfg.setId(cfgId);
                    studentBehaviorCfg.setStatus(1);
                    cfgDao.updateById(studentBehaviorCfg);
                }
                cfgClassDao.createCfgClassBatch(cfgId,classIdList);
                return true;
            }
        }
        return false;
    }

    @Override
    public List<StudentBehaviorCfgClass> findClassByCfgId(Long cfgId) {
        QueryWrapper<StudentBehaviorCfgClass> wrapper = new QueryWrapper<>();
        wrapper.eq("cfg_id",cfgId);
        return cfgClassDao.selectList(wrapper);
    }

    @Override
    public List<StudentBehaviorCfg> findCfgByClassId(Long classId) {
        QueryWrapper<StudentBehaviorCfgClass> wrapper = new QueryWrapper<>();
        wrapper.select("cfg_id");
        wrapper.eq("class_id",classId);
        List<StudentBehaviorCfgClass> cfgClassList = cfgClassDao.selectList(wrapper);
        List<Long> cfgIdList = new ArrayList<>();
        for(StudentBehaviorCfgClass cfgClass : cfgClassList){
            cfgIdList.add(cfgClass.getCfgId());
        }
        QueryWrapper<StudentBehaviorCfg> cfgWrapper = new QueryWrapper<>();
        cfgWrapper.in("id",cfgIdList);
        return cfgDao.selectList(cfgWrapper);
    }

}
